package com.wang.homework;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

import com.wang.domain.Student;

public class UnZip {
	
	private static Student student;

	public UnZip(Student s) {
		student = s;
	}
	/**
	 * 解压zip文件
	 * @param source
	 * @return
	 * @throws FileNotFoundException
	 */
	public static File unzip(File source) throws FileNotFoundException{
		if(!source.exists()){
			throw new FileNotFoundException("目标文件不存在");
		}
		String str = source.getParent()+"/"+source.getName().substring(0, source.getName().lastIndexOf('.'));
		File dest = new File(str);
		dirNotFound(dest);
		ZipFile zipFile = null;
		InputStream inputStream = null;
		FileOutputStream fileOutputStream = null;
		try {
			//创建ZipFile对象的时候一定要注意编码问题不然报错
			zipFile = new ZipFile(source,Charset.forName("GBK"));
			Enumeration<? extends ZipEntry> entries = zipFile.entries();
			while(entries.hasMoreElements()){
				ZipEntry nextElement = entries.nextElement();
				if(nextElement.isDirectory()){
					//如果是文件夹就拷贝到指定的文件夹下
					dirNotFound(dest.getPath()+"/"+nextElement.getName());
				}else{
					//如果是文件就拷贝到目标文件夹下
					File file = new File(dest+"/"+nextElement.getName());
					ifParentDirNotExist(file);
					inputStream = zipFile.getInputStream(nextElement);
					fileOutputStream = new FileOutputStream(file);
					byte[] b = new byte[1024];
					int len;
					while((len = inputStream.read(b)) != -1){
						fileOutputStream.write(b, 0, len);
					}
				}
			}
		} catch (ZipException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			try {
				if(fileOutputStream != null)
					fileOutputStream.close();
				if(fileOutputStream != null)
					inputStream.close();
				if(zipFile != null)
					zipFile.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		return dest;
	}
	private static void ifParentDirNotExist(File file) {
		dirNotFound(file.getParentFile());
	}
	private static void dirNotFound(String s){
		File file = new File(s);
		dirNotFound(file);
	}
	private static void dirNotFound(File file) {
		file.mkdirs();
	}
	/**
	 * 遍历目录返回文件夹的大小
	 * @param source
	 * @return
	 */
	public static void foreachDir(File source) {
		if(source.isDirectory()){
			File[] listFiles = source.listFiles();
			for (File file : listFiles) {
				if(file.isDirectory()){
					if("代码".equals(source.getName())){
						copyFile(source,new File("F:/homework/"+new SimpleDateFormat("yyyy-MM-DD").format(new Date())+"/代码/"+student.getUsername()));
						continue;
					}else if("总结".equals(source.getName())){
						copyFile(source,new File("F:/homework/"+new SimpleDateFormat("yyyy-MM-DD").format(new Date())+"/总结/"+student.getUsername()));
						continue;
					}
					foreachDir(file);
				}
			}
		}
	}
	public static Long getSize(File source) {
		Long len = 0L;
		if(source.isDirectory()){
			File[] listFiles = source.listFiles();
			for (File file : listFiles) {
				if(file.isDirectory()){
					len += getSize(file);
				}else{
					len += file.length();
				}
			}
		}else{
			return source.length();
		}
		return len;
	}
	
	
	/**
	 * 拷贝文件或文件夹到指定文件夹下
	 * @param source
	 * @param dest
	 */
	public static void copyFile(File source,File dest){
		if(dest.isFile()){
			throw new IllegalArgumentException("参数不合法");
		}else{
			dirNotFound(dest);
		}
		if(source.isFile()){
			BufferedReader bufferedReader = null;
			BufferedWriter bufferedWriter = null;
			try {
				FileInputStream fileInputStream = new FileInputStream(source);
				bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
				bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(dest, source.getName()))));
				String str = null;
				while((str = bufferedReader.readLine()) != null){
					bufferedWriter.write(str);
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally{
				try {
					if(bufferedWriter != null)
						bufferedWriter.close();
					if(bufferedReader != null)
						bufferedWriter.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}else{
			dest = new File(dest.getPath()+"/"+source.getName());
			dirNotFound(dest);
			File[] listFiles = source.listFiles();
			for (File file : listFiles) {
				copyFile(file, dest);
			}
		}
	} 
	/**
	 * 删除文件夹
	 * @param source
	 */
	public static void deleteDir(File source) {
		if(source.isDirectory()){
			File[] listFiles = source.listFiles();
			for (File file : listFiles) {
				deleteDir(file);
			}
		}
		source.delete();
	}
	
}
